//
//  newsViewController.m
//  movie
//
//  Created by 李丹阳 on 15/7/22.
//  Copyright (c) 2015年 李丹阳. All rights reserved.
//

#import "newsViewController.h"
#import "dataService.h"
#import "UIViewExt.h"
#import "NewsImageListViewController.h"

#define Kwidth  self.view.bounds.size.width
#define Kheight self.view.bounds.size.height

@interface newsViewController (){
    NSMutableArray* modalArray;
    newsModal* headerModal;
    UIImageView* bgImageView;
    UILabel* titleLabel;
}

@end

@implementation newsViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self) {
        self.title = @"新闻";
    }
    return self;
}
- (void)viewDidAppear:(BOOL)animated{
    
    NSString *str = NSStringFromUIEdgeInsets([_newsTableView contentInset]);
    NSLog(@"%@",str);
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.edgesForExtendedLayout = UIRectEdgeAll;
    modalArray = [[NSMutableArray alloc]init];
    [self loadData];
    [self _createTabelView];
    [self _createTableHeaderView];

    // Do any additional setup after loading the view.
}

- (void)loadData{
    NSArray* dataArray = [dataService getJSONDataFromFile:@"news_list"];
    for (NSDictionary* dic in dataArray) {
        newsModal* modal = [[newsModal alloc]init];
        [modal setValuesForKeysWithDictionary:dic];//将dic内的值根据相同key名复制到modal中
        [modalArray addObject:modal];
    }
    headerModal = [modalArray objectAtIndex:0];
    [modalArray removeObjectAtIndex:0];
//    NSLog(@"%@",modalArray);
}

- (void)_createTabelView{
    _newsTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, Kheight)];
    _newsTableView.delegate = self;
    _newsTableView.dataSource = self;
    [self.view addSubview:_newsTableView];
    UINib* nib = [UINib nibWithNibName:@"newsTableViewCell" bundle:nil];
    [_newsTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
}

- (void)_createTableHeaderView{
    UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
    headerView.backgroundColor = [UIColor clearColor];
    _newsTableView.tableHeaderView = headerView;
    
    bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, 200)];
    [bgImageView sd_setImageWithURL:[NSURL URLWithString:headerModal.image]];
    
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 170, Kwidth, 30)];
    titleLabel.text = headerModal.title;
    titleLabel.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.3];
    [self.view addSubview:bgImageView];
    [self.view addSubview:titleLabel];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return modalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    newsTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.modal = modalArray[indexPath.row];
    UIImageView * imgeView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tab_bg_all@2x"]];
    imgeView.frame = cell.contentView.frame;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   // self.edgesForExtendedLayout
    cell.backgroundView = imgeView;

    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSizeY = scrollView.contentOffset.y;
    NSLog(@"%f",offSizeY);
    if (offSizeY > 0) {
        bgImageView.top = -offSizeY;
        titleLabel.bottom = bgImageView.bottom;
    }
    else{
        CGFloat newHeigh = 200 + ABS(offSizeY);
        CGFloat newWidth = newHeigh*Kwidth/200;
        bgImageView.frame = CGRectMake( (Kwidth-newWidth)/2 , 0 , newWidth, newHeigh);
        titleLabel.bottom = bgImageView.bottom;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsImageListViewController* vc = [[NewsImageListViewController alloc]init];
    [self.navigationController pushViewController:vc
                                         animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
